package com.qianbei.common.net.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import com.qianbei.common.R;


public class ProgressDialog extends BaseProgressDialog implements
		OnClickListener, OnKeyListener {

	private Dialog dialog;
	private View rateView;
	private View contentView;

	public ProgressDialog(Context context) {
		dialog = new Dialog(context, R.style.kit_progress_dialog);// 创建自定义样式dialog
		contentView = View.inflate(context, R.layout.comm_progress, null);
		dialog.setContentView(contentView);
		dialog.setCanceledOnTouchOutside(false);
		dialog.setOnKeyListener(this);
//		dialog.findViewById(R.id.close).setOnClickListener(this);
		dialog.findViewById(R.id.progress)
				.startAnimation(
						AnimationUtils.loadAnimation(context,
								R.anim.comm_anim_progress));
		rateView = dialog.findViewById(R.id.progress_rate);
	}

	@Override
	public void progressRate(int value) {
		LayoutParams lp = rateView.getLayoutParams();
		lp.width = value * contentView.getWidth() / 100;
		rateView.setLayoutParams(lp);
	}

	@Override
	public void showProgress(String describe) {
		if (describe == null) {
			describe = "正在加载...";
		}
		((TextView) dialog.findViewById(R.id.progress_text)).setText(describe);
		dialog.show();
	}

	@Override
	public void progressFinish() {
		close();
	}

	public void hide() {
		if (dialog != null) {
			dialog.hide();
		}
	}

	public void close() {
		if (dialog != null) {
			dialog.cancel();
		}
	}

	@Override
	public void onClick(View v) {
		userBreak();
		close();
	}

	@Override
	public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_SEARCH) {
			userBreak();
		}
		return false;
	}
}
